package com.xinsoft.controller.mes;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.xinsoft.aop.ano.SysLog;
import com.xinsoft.common.BusinessType;
import com.xinsoft.common.Response;
import com.xinsoft.constant.Const;
import com.xinsoft.entity.dto.MesProcessPlan2DTO;
import com.xinsoft.entity.dto.MesProcessPlanDTO;
import com.xinsoft.entity.dto.MesProcessPlanParam;
import com.xinsoft.entity.po.MesProcessPlan;
import com.xinsoft.entity.po.MesWorkTeam;
import com.xinsoft.service.*;
import com.xinsoft.validator.ValidatorUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName MesProcessPlanController
 * @Description 工序计划
 * @Author hsm
 * @Date 22023-7-24 14:10:36
 * @Version 1.0
 */
@RestController
@RequestMapping("/mes/processPlan")
@Api(value = "MesProcessPlanController", tags = {"工序计划"})
public class MesProcessPlanController {

    @Autowired
    private MesProcessPlanService mesProcessPlanService;

    @Autowired
    private MesProcessPlanDetailService mesProcessPlanDetailService;

    /**
     * @return com.xinsoft.common.Response<?>
     * @Author hsm
     * @Description 更新
     * @Date 2023/5/17 9:57
     * @params dept
     **/
    @ApiOperation("变更")
    @SysLog(title = "工序计划-编辑", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> edit(@RequestBody MesProcessPlanDTO planDTO) {
        // 参数校验
        ValidatorUtils.validateEntity(planDTO);
        MesProcessPlan processPlanDO = mesProcessPlanService.getById(planDTO.getId());
        if (ObjectUtil.isNotEmpty(processPlanDO) && !processPlanDO.getProcessStatus().equals(Const.producePlanStatus.PLAN)){
            return Response.fail("该单据状态不允许修改！");
        }
        mesProcessPlanService.edit(planDTO);
        return Response.succeed();
    }

    /**
     * @return com.xinsoft.common.Response<?>
     * @Author hsm
     * @Description 获取详情
     * @Date 2023/5/17 9:57
     * @params id
     **/
    @ApiOperation("获取详情")
    @GetMapping("/detail")
    public Response<?> detail(@RequestParam(value = "id", defaultValue = "", required = true) Integer id) {
        MesProcessPlanDTO result = mesProcessPlanService.getInfoById(id);
        MesProcessPlan2DTO result2 = new MesProcessPlan2DTO();
        result2.setDeptId(ObjectUtil.isEmpty(result.getDeptId()) ? "" : result.getDeptId().toString());
        BeanUtils.copyProperties(result,result2);
        return Response.succeed(result2);
    }

    /**
     * @return com.xinsoft.common.Response<?>
     * @Author hsm
     * @Description 变更打印状态
     * @Date 2023/5/17 9:57
     * @params id
     **/
    @ApiOperation("变更打印状态")
    @GetMapping("/changePrintStatus")
    public Response<?> changePrintStatus(@RequestParam(value = "id", defaultValue = "", required = true) Integer id) {
        MesProcessPlanDTO result = mesProcessPlanService.getInfoById(id);
        result.setPrintStatus(Const.Y);
        mesProcessPlanService.updateById(result);
        return Response.succeed(result);
    }

    /**
     * @return com.xinsoft.common.Response<?>
     * @Author hsm
     * @Description 变更插单状态
     * @Date 2023-9-21 13:36:32
     * @params id
     **/
    @ApiOperation("变更插单状态")
    @GetMapping("/changeProcessStatus")
    public Response<?> changeProcessStatus(@RequestParam(value = "id", defaultValue = "", required = true) Integer id) {
        MesProcessPlanDTO result = mesProcessPlanService.getInfoById(id);
        result.setProcessStatus(Const.producePlanStatus.ORDER_INSERTING);
        mesProcessPlanService.updateById(result);
        return Response.succeed(result);
    }

    /**
     * @return com.xinsoft.common.Response<?>
     * @Author hsm
     * @Description 删除
     * @Date 2023/5/12 9:20
     * @params ids
     **/
    @ApiOperation("删除")
    @SysLog(title = "工序计划-删除", businessType = BusinessType.DELETE)
    @PostMapping("/del")
    public Response<?> del(@RequestBody List<Integer> ids) {
        mesProcessPlanService.removeByIds(ids);
        return Response.succeed();
    }

    /**
     * @return com.xinsoft.common.Response<?>
     * @Author hsm
     * @Description 工序计划-分页
     * @Date 2023/5/18 10:51
     * @params param
     **/
    @PostMapping("/pageList")
    public Response<?> pageList(@Valid @RequestBody MesProcessPlanParam param) {
        return Response.succeed(mesProcessPlanService.findPageListByParam(param));
    }

    @PostMapping("/findTotalByParam")
    public Response<?> findTotalByParam(@Valid @RequestBody MesProcessPlanParam param) {
        return Response.succeed(mesProcessPlanService.findTotalByParam(param));
    }

    /**
     * 下达工序计划
     * @return com.xinsoft.common.Response<?>
     * @Author hsm
     * @Description 下达工序计划
     * @Date 2023/5/18 10:51
     * @params param
     **/
    @Transactional(rollbackFor = Exception.class)
    @PostMapping(value = "/issuedPlans")
    public Response<?>  issuedPlans(@RequestBody List<Integer> ids) {
        List<MesProcessPlan> planList = mesProcessPlanService.list(new LambdaQueryWrapper<MesProcessPlan>().in(MesProcessPlan::getId, ids));
        List<MesProcessPlan> errors2 = planList.stream().filter(item -> !Const.producePlanStatus.PLAN.equals(item.getProcessStatus())).collect(Collectors.toList());
        if (ObjectUtil.isNotEmpty(errors2)){
            return Response.fail("存在非计划状态单据！");
        }
        return mesProcessPlanService.issuedPlans(ids);
    }

    /**
     * 反下达
     * @return com.xinsoft.common.Response<?>
     * @Author hsm
     * @Description 反下达工序计划
     * @Date 2023/5/18 10:51
     * @params param
     **/
    @Transactional(rollbackFor = Exception.class)
    @PostMapping(value = "/reverseIssuedPlan")
    public Response<?>  reverseIssuedPlan(@RequestBody List<Integer> ids) {
        mesProcessPlanService.reverseIssuedPlan(ids);
        return Response.succeed();
    }

    /**
     * 结案
     * @return com.xinsoft.common.Response<?>
     * @Author hsm
     * @Description 反结案工序计划
     * @Date 2023/5/18 10:51
     * @params param
     **/
    @Transactional(rollbackFor = Exception.class)
    @PostMapping(value = "/close")
    public Response<?>  close(@RequestBody List<Integer> ids) {
        mesProcessPlanService.close(ids);
        return Response.succeed();
    }

    /**
     * 反结案
     * @return com.xinsoft.common.Response<?>
     * @Author hsm
     * @Description 反结案工序计划
     * @Date 2023/5/18 10:51
     * @params param
     **/
    @Transactional(rollbackFor = Exception.class)
    @PostMapping(value = "/reverseClose")
    public Response<?>  reverseClose(@RequestBody List<Integer> ids) {
        mesProcessPlanService.reverseClose(ids);
        return Response.succeed();
    }


    /**
     * @return com.xinsoft.common.Response<?>
     * @Author hsm
     * @Description 加急
     * @Date 2023/5/17 9:57
     * @params id
     **/
    @ApiOperation("加急")
    @GetMapping("/urgent")
    public Response<?> urgent(@RequestParam(value = "detailId", defaultValue = "", required = true) Integer detailId) {
        mesProcessPlanDetailService.urgent(detailId);
        return Response.succeed();
    }
}
